//
//  Team9RefreshTool.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/22.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9RefreshTool.h"
#import <MJRefresh/MJRefresh.h>
#import "JQAFNetworkTool.h"
#import "Team9EmptyTool.h"
#import <MBProgressHUD.h>

@interface Team9RefreshTool ()

@property(nonatomic, assign) NSInteger page;
@property(nonatomic, strong) NSString *url;
@property(nonatomic, copy) NSString *orignUrl;
@property(nonatomic, strong) NSMutableArray *array;


@end
@implementation Team9RefreshTool
/*
 申请数据有两个共同点
 解析的类型的方法一样
 url page++
 把数据解析的部分取出来
 把数据的总数组返回
 刷新和加载对数组的处理在这里内部完成
 用url 和 page array 记录数据
 */
- (instancetype)initWithScrollView:(UIScrollView *)scrollView
                               url:(NSString *)url
                           style:(NSArray *(^)(id result))style
                           refresh:(void(^)(NSArray *array))refresh
                             empty:(Team9EmptyTool *)empty
{
    self = [super init];
    if (self) {
        self.orignUrl = url;
        refresh([NSArray array]);
        [scrollView.footer resetNoMoreData];
        void(^block)() = ^{
            scrollView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                NSRange range = [self.url rangeOfString:[NSString stringWithFormat:@"page=%ld", self.page]];
                if (!range.length) {
                    [scrollView.footer endRefreshingWithNoMoreData];
                    return;
                }
                self.url = [self.url stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"page=%ld", self.page + 1]];
                [JQAFNetworkTool getUrl:self.url success:^(id result) {
                    NSArray *array = style(result);
                    if (array.count) {
                        [self.array addObjectsFromArray:array];
                        array = [NSArray arrayWithArray:self.array];
                        refresh(array);
                        //                    completion(array);
                        self.page++;
                        [scrollView.footer endRefreshing];
                    } else {
                        [scrollView.footer endRefreshingWithNoMoreData];
                    }
                }failure:^(NSError *error) {
                    [scrollView.header endRefreshing];
                }];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [scrollView.footer endRefreshing];
                });
            }];
        };
        [JQAFNetworkTool getUrl:self.orignUrl success:^(id result) {
            NSArray *array = style(result);
            self.array = array.mutableCopy;
            refresh(array);
            block();
            }];
   
        scrollView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            empty.loading = YES;
            [scrollView.footer resetNoMoreData];
            [JQAFNetworkTool getUrl:self.orignUrl success:^(id result) {
                NSArray *array = style(result);
                self.array = array.mutableCopy;
                refresh(array);
                self.page = 1;
                self.url = self.orignUrl;
                [scrollView.header endRefreshing];
                block();
            } failure:^(NSError *error) {
                [scrollView.header endRefreshing];
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [scrollView.header endRefreshing];
            });
        }];
       
    }
    return self;
}
+ (Team9RefreshTool *)toolWithScrollView:(UIScrollView *)scrollView
                                     url:(NSString *)url
                                   style:(NSArray *(^)(id result))style
                                 refresh:(void(^)(NSArray *array))refresh
                                   empty:(Team9EmptyTool *)empty{
    Team9RefreshTool *tool = [[Team9RefreshTool alloc] initWithScrollView:scrollView url:url style:style refresh:refresh empty:empty];
    return tool;
}
- (void)setOrignUrl:(NSString *)orignUrl
{
    if (_orignUrl != orignUrl) {
        _orignUrl = orignUrl;
    }
    self.page = 1;
    self.url = self.orignUrl;
}
+ (void)getUrl:(NSString *)url
         success:(void (^)(id result))success
      scrollView:(UIScrollView *)scrollView
         empty:(Team9EmptyTool *)empty{
    [JQAFNetworkTool getUrl:url success:^(id result) {
        success(result);
    }];
    scrollView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        empty.loading = YES;
        [JQAFNetworkTool getUrl:url success:^(id result) {
            success(result);
            [scrollView.header endRefreshing];
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [scrollView.header endRefreshing];
        });
    }];
    
}
@end
